<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 200px;
            height: 200px;
            background: green;
        }
        
        .active {
            background: red;
            color: yellow;
            border: 5px solid orange;
            width: 200px;
            height: 200px;
        }
    </style>
</head>

<body>
    <button>显示或隐藏</button>
    <div id="box" class="box">我是div</div>
    <script>
        //1.获取元素
        var oBox = document.getElementById("box");
        console.dir(oBox);
        var oBtn = document.querySelector("button");
        var type = true;
        //2.给 按钮绑定 点击事件
        oBtn.onclick = function() {
                //给 oBox的 style 里面的 display属性设置新值
                //style  设置的 行内样式,如果样式比较多时，建议使用 className
                if (type) {
                    oBox.style.display = "none";
                } else {
                    oBox.style.display = "";
                    //className 设置这个dom节点的class名字
                    // oBox.className = "active";
                    oBox.style.background = random_color(6);
                    oBox.style.color = random_color(6);
                    oBox.style.border = "5px solid" + random_color(6);

                }
                type = !type;
            }
            //作业2：点击随机切换颜色
            // 随机生成颜色函数 
            // 参数 n  生成的随机颜色的位数
        function random_color(n) {
            let color_arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
            let arr = []
            for (let i = 0; i < n; i++) {
                let random = Math.floor(Math.random() * (15 - 1 + 1) + 1);
                if (arr.indexOf(random) == -1) {
                    arr.push(random);
                } else {
                    i--;
                }

            }
            let sum = "#";
            for (let i = 0; i < arr.length; i++) {
                sum += color_arr[arr[i]];
            }
            return sum;
        }
        // random_color(6);
        console.log(random_color(6));
    </script>
</body>

</html>